﻿using System.Diagnostics;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Windows.Forms;
using System;

namespace SopCastRecorder
{
	public class OptionFile
	{
		public static string FileName = Application.StartupPath + @"\Option.config";

		private static Option _Option1;
		public static Option Option1
		{
			get
			{
				if (_Option1 == null)
				{
					Read();
				}
				return _Option1;
			}
			set
			{
				_Option1 = value;
			}
		}

		public static void Read()
		{
			try
			{
				if (File.Exists(FileName))
				{
					var serializer = new XmlSerializer(typeof(Option));
					using (var fs = new FileStream(FileName, FileMode.Open))
					{
						Option1 = (Option)serializer.Deserialize(fs);
						fs.Close();
					}
				}
				else
				{
					Option1 = new Option();
				}
			}
			catch
			{
				Option1 = new Option();
			}
		}
		public static void Write()
		{
			var serializer = new XmlSerializer(typeof(Option));
			using (var fs = new FileStream(FileName, FileMode.Create))
			{
				var settings = new XmlWriterSettings()
				{
					Indent = true,
					Encoding = Encoding.Unicode
				};
				using (var writer = XmlWriter.Create(fs, settings))
				{
					serializer.Serialize(writer, Option1);
					writer.Close();
					fs.Close();
				}
			}
		}

		public static void Validation(Action<bool> callback)
		{
			bool isSopCastPath = File.Exists(Option1.SopCastPath);
			callback.Invoke(isSopCastPath);
		}
	}
}
